package com.zxe.noticeserver.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zxe.commonserver.constant.MessageConstant;
import com.zxe.commonserver.constant.StatusConstant;
import com.zxe.commonserver.exception.DeletionNotAllowedException;
import com.zxe.commonserver.result.PageResult;
import com.zxe.noticeserver.mapper.NoticeMapper;
import com.zxe.noticeserver.pojo.Notice;
import com.zxe.noticeserver.pojo.NoticeDTO;
import com.zxe.noticeserver.pojo.NoticePageQueryDTO;
import com.zxe.noticeserver.pojo.NoticeVO;
import com.zxe.noticeserver.service.NoticeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    /**
     * 新增公告
     * @param noticeDTO
     */
    public void addNotice(NoticeDTO noticeDTO) {
        //向公告表插入数据
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeDTO, notice);
        noticeMapper.insert(notice);
    }

    /**
     * 公告分页查询
     * @param noticePageQueryDTO
     * @return
     */
    public PageResult pageQuery(NoticePageQueryDTO noticePageQueryDTO) {
        PageHelper.startPage(noticePageQueryDTO.getPage(), noticePageQueryDTO.getPageSize());
        Page<NoticeVO> page = noticeMapper.pageQuery(noticePageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 批量删除公告
     * @param ids
     */
    public void delete(List<Long> ids) {

        for (Long id : ids) {
            //删除公告表数据
            noticeMapper.delete(id);
        }
    }

    /**
     * 修改公告
     * @param noticeDTO
     */
    public void update(NoticeDTO noticeDTO) {

        //更新公告表
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeDTO, notice);
        noticeMapper.updateById(notice);
    }

    /**
     * 设置公告可见范围
     * @param status
     * @param id
     */
    public void openOrHide(Integer status, Long id) {

        Notice notice = Notice.builder()
                .status(status)
                .id(id)
                .build();
        noticeMapper.updateById(notice);
    }

    /**
     * 根据id查询公告
     * @param id
     * @return
     */
    public NoticeVO findById(Long id) {

        Notice notice = noticeMapper.getById(id);
        NoticeVO noticeVO = new NoticeVO();
        BeanUtils.copyProperties(notice, noticeVO);
        return noticeVO;
    }
}
